package zhongbo.java.air.dictionary.service.serverImpl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.exception.NoStackLogException;
import zhongbo.java.air.common.util.ObjectIsNullUtil;
import zhongbo.java.air.dictionary.mapper.AirCraftMapper;
import zhongbo.java.air.dictionary.model.AirCraft;
import zhongbo.java.air.dictionary.model.Dto.PageDto;
import zhongbo.java.air.dictionary.service.IAirCraftService;

import java.util.Arrays;

@Service
public class IAirCraftServiceImpl implements IAirCraftService {

    @Autowired
    private AirCraftMapper airCraftMapper;

    //增加
    @Override
    public void addAirCraft(AirCraft airCraft) {

        boolean b = ObjectIsNullUtil.allNonIdFieldsNonNull(airCraft);
        if(!b){
            throw new NoStackLogException("关键属性不能为空");
        }
        if(airCraft.getIcaoCode().length()>6){
            throw new NoStackLogException("地址码超过6位");
        }

        LambdaQueryWrapper<AirCraft> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AirCraft::getIcaoCode,airCraft.getIcaoCode());
        AirCraft airCraft1 = airCraftMapper.selectOne(lqw);
        if(airCraft1!=null){
            throw new NoStackLogException("ICAO 24位地址码重复");
        }

        LambdaQueryWrapper<AirCraft> lqwtwo = new LambdaQueryWrapper<>();
        lqwtwo.eq(AirCraft::getRegistration,airCraft.getRegistration());
        AirCraft airCraft2 = airCraftMapper.selectOne(lqwtwo);
        if(airCraft2!=null){
            throw new NoStackLogException("飞机注册号重复");
        }

        airCraftMapper.insert(airCraft);
    }

    //删除
    @Override
    public void deleteAirCraftById(String id) {

        if(id==null||id.isEmpty()){
            throw new NoStackLogException("id不能位空");
        }

        LambdaQueryWrapper<AirCraft> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AirCraft::getId,id);
        airCraftMapper.delete(lqw);
    }

    //修改
    @Override
    public void updateAirCraft(AirCraft airCraft) {

        if(airCraft.getId()==null||airCraft.getId().isEmpty()){
            throw new NoStackLogException("id不能位空");
        }

        if(airCraft.getIcaoCode()==null||airCraft.getIcaoCode().length()>6){
            throw new NoStackLogException("地址码超过6位");
        }

        LambdaQueryWrapper<AirCraft> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AirCraft::getId,airCraft.getId());
        airCraftMapper.update(airCraft,lqw);
    }

    //分页查询，可排序
    @Override
    public Page<AirCraft> selectPage(PageDto airCraftPageDto) {

        boolean get = Arrays.stream(airCraftPageDto.getClass().getDeclaredMethods())
                .filter(method -> method.getName().startsWith("get"))
                .map(method -> {
                    try {
                        return method.invoke(airCraftPageDto);
                    } catch (Exception e) {
                        return null;
                    }
                })
                .noneMatch(value -> value == null);
        if(get){
            throw new NoStackLogException("关键属性不能为空");
        }

        QueryWrapper<AirCraft> lqw = new QueryWrapper<>();
        lqw.orderBy(true,airCraftPageDto.isAsc(),airCraftPageDto.getSortField());
        Page<AirCraft> page = new Page(airCraftPageDto.getPageNum(), airCraftPageDto.getPageSize());
        Page<AirCraft> airCraftPage = airCraftMapper.selectPage(page, lqw);
        return airCraftPage;
    }

}
